package com.czk.model.strategy.spring.controller;

import com.czk.model.strategy.spring.common.Order;
import com.czk.model.strategy.spring.common.PaymentResult;
import com.czk.model.strategy.spring.strategy.Strategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/5/19 17:00
 */
@RestController
@RequestMapping("/payment")
public class PaymentController {
    @Autowired
    private ApplicationContext applicationContext;

    @GetMapping("/pay")
    public PaymentResult paymentResult(@RequestParam("amount") int amount, @RequestParam("paymentType") String paymentType) {
        // 构建订单
        Order order = new Order(paymentType, amount);
        // 拿到支付类型bean
        Strategy strategy=null;
        try {
             strategy = applicationContext.getBean(paymentType, Strategy.class);
        }catch (Exception ex){
            return new PaymentResult("没有该支付方式，请重试");
        }

        // 支付
        PaymentResult pay = strategy.pay(order);
        return pay;
    }
}
